# ! /usr/bin/python3
# coding=utf-8
# @time: 2025/10/30 16:28
# @Author:鹤川
import re

from airtest.core.api import *
from common.conf_path import pic_voice
from common.doFile import myLogger
from pages.basePage import BasePage


class JiaZiPage(BasePage):
    eadd = "com.kkpodcast:id/create_clip_iv"
    eadd_text = "com.kkpodcast:id/content_et"
    econfirm = "com.kkpodcast:id/confirm_tv"

    echange = "com.kkpodcast:id/edit_clip_iv"
    eedit = "com.kkpodcast:id/edit_iv"
    echange_text = "com.kkpodcast:id/content_et"
    econfirm_change = "com.kkpodcast:id/confirm_tv"
    eback = "com.kkpodcast:id/back_iv"
    enum = "com.kkpodcast:id/album_clip_num_tv"

    def clean_number_text(self, text):
        """清理文本中的非数字字符，提取纯数字"""
        if not text:
            return 0
        # 使用正则表达式提取数字
        numbers = re.findall(r'\d+', text)
        if numbers:
            return int(numbers[0])
        else:
            print(f"警告: 无法从文本 '{text}' 中提取数字")
            return 0


    def addJia(self,eadd_text):
        num_text = self.getText(self.enum)
        self.begin_num = self.clean_number_text(num_text)
        print(f"添加前夹子数量: '{self.begin_num}'")
        myLogger.info(f"添加前夹子数量: '{self.begin_num}'")

        # 点击加号
        self.clickEle(self.eadd)
        myLogger.info("点击加号")
        # self.clickEle(self.eadd_text)
        # 选择输入夹子输入框，输入内容
        self.inputEle(self.eadd_text, eadd_text)
        time.sleep(1)
        myLogger.info("输入夹子内容")
        time.sleep(1)
        # 点击确认按钮
        self.clickEle(self.econfirm)
        myLogger.info("点击确认按钮")
        return self

    def change_jia(self,echange_text):
        self.clickEle(self.echange)
        self.clickEle(self.eedit)
        self.inputEle(self.echange_text, echange_text)
        self.clickEle(self.econfirm_change)
        time.sleep(2)
        self.clickEle(self.eback)
        return self


    def addJZOK(self,t):
        self.addJia(t)
        # 添加后获取夹子数量，并清理文本
        num_text = self.getText(self.enum)
        self.end_num = self.clean_number_text(num_text)
        print(f"添加后夹子数量: {self.end_num}")
        myLogger.info(f"添加后夹子数量: {self.end_num}")
        # 判断是否添加成功
        if self.end_num == self.begin_num + 1:
            print(f"夹子添加成功! 数量从 {self.begin_num} 增加到 {self.end_num}")
            myLogger.info(f"夹子添加成功! 数量从 {self.begin_num} 增加到 {self.end_num}")
            return True, self.begin_num, self.end_num
        else:
            print(f"夹子添加失败! 同名夹子不添加，数量仍然是 {self.begin_num}")
            myLogger.error(f"夹子添加失败! 同名夹子不添加，数量仍然是 {self.begin_num}")
            return False, self.begin_num, self.end_num
    def changeJZOK(self):
        # 判断是否修改成功
        pic = os.path.join(pic_voice, "tpl1761996898983.png")
        evoice = Template(pic, record_pos=(0.017, 0.746), resolution=(900, 1600),threshold=0.35)
        if not exists(evoice):
            print("修改成功")
            myLogger.info("修改成功")
            return 1
        else:
            print("修改失败")
            myLogger.error("修改失败")
            return 0



if __name__ == '__main__':
    # JiaZiPage().addJia("汤超超")
    # JiaZiPage().change_jia("汤超超")
    JiaZiPage().changeJZOK("3")





